<?php
//用户管理

namespace App\Http\Controllers\Manage;

use App\Http\Requests\Manage\User\SaveRequest;
use App\Models\ManageActionLogModel;
use App\Services\Manage\UserManageService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use Illuminate\Support\Facades\Auth;

class UserController extends BaseController
{

    /**
     * 用户授权登录
     * @param  Request  $request
     * @return JsonResponse
     */
    public function login(Request $request)
    {
        $username = $request->post('username', '');
        $password = $request->post('password', '');

        $res = UserManageService::login($username, $password);
        if ($res['errcode']) {
            return $this->error(401, $res['errmsg']);
        }

        return $this->success($res['data']);
    }

    /**
     * 登出
     * @param  Request  $request
     * @return JsonResponse
     */
    public function logout(Request $request)
    {
        $user = $request->user();

        // 撤销所有令牌...
        $user->tokens()->delete();

        return $this->success();
    }

    /**
     * 修改密码
     * @param  Request  $request
     * @return JsonResponse
     */
    public function updatePassword(Request $request)
    {
        $oldPassword = $request->post('original_password', '');
        $newPassword = $request->post('new_password', '');

        $res = UserManageService::updatePassword($request->user()->id, $oldPassword, $newPassword);
        if ($res['errcode']) {
            return $this->error(401, $res['errmsg']);
        }

        return $this->success($res['data']);
    }

    /**
     * 获取用户信息
     * @param  Request  $request
     * @return JsonResponse
     */
    public function loginUserInfo(Request $request)
    {

        $data = $request->user()->toarray();

        return $this->success([
            'id'       => $data['id'],
            'username' => $data['username'],
            'type'     => $data['type'],
        ]);
    }

    /**
     * 管理员用户列表
     * @param  Request  $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        $query = $request->all();
        $page = request('page', 1);
        $pageSize = request('pageSize', 20);

        $list = UserManageService::getList($query, $page, $pageSize);

        return $this->success($list);
    }

    /**
     * 保存用户
     * @param  Request  $request
     * @return JsonResponse
     */
    public function save(SaveRequest $request)
    {
        if ($request->user()->type != 1) {
            return $this->error(401, '无权限');
        }
        $res = UserManageService::save(
            $request->post('id', 0),
            $request->post('username',),
            $request->post('password',),
            $request->post('mobile'),
            $request->post('email'),
        );
        if ($res['errcode']) {
            return $this->error(401, $res['errmsg']);
        }

        return $this->success($res['data']);
    }

    /**
     * 编辑回显信息
     * @param  Request  $request
     * @return JsonResponse
     */
    public function editShow(Request $request)
    {
        $id = $request->get('id', 0);

        $res = UserManageService::editShow($id);

        return $this->success($res);
    }

    /**
     * 修改用户状态
     * @param  Request  $request
     * @return JsonResponse
     */
    public function updateStatus(Request $request)
    {
        if ($request->user()->type != 1) {
            return $this->error(401, '无权限');
        }
        $id = $request->post('id', 0);
        $status = $request->post('status', 0);

        $res = UserManageService::updateStatus($id, $status);
        if ($res['errcode']) {
            return $this->error(401, $res['errmsg']);
        }

        return $this->success($res['data']);
    }


}
